package com.bigger.arch.app.web.controller;

import com.bigger.arch.app.biz.IDemoBiz;
import com.bigger.arch.app.biz.IDemoXmlBiz;
import com.bigger.arch.app.dto.UserDto;
import com.bigger.arch.app.web.aop.BiggerExtrable;
import com.bigger.arch.app.web.aop.test.DemoClient;
import com.bigger.arch.app.web.aop.test.HelloRequestType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

//@RestController //相当于 @Controller+@ResponseBody
@Controller
@RequestMapping("/demo")
//@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class DemoController {

    private enum TestEnum {
        One("一", 1),
        Two("二", 2),
        Four("四", 4),
        Eight("八", 8),
        Sixteen("十六", 16);

        private String desc;
        private int value;

        TestEnum(String desc, int value) {
            this.desc = desc;
            this.value = value;
        }

        public String getDesc() {
            return desc;
        }

        public int getValue() {
            return value;
        }
    }

    private static class MyHashMap<K, V> extends HashMap<K, V> {
        @Override
        protected void finalize() throws Throwable {
            System.out.println("HashMap gc回收");
            super.finalize();
        }
    }

    private static class MyEntity {
        private String s1;
        private int i1;
        private Object obj;

        public String getS1() {
            return s1;
        }

        public void setS1(String s1) {
            this.s1 = s1;
        }

        public int getI1() {
            return i1;
        }

        public void setI1(int i1) {
            this.i1 = i1;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        @Override
        protected void finalize() throws Throwable {
            System.out.println("MyEntity gc回收");
            super.finalize();
        }
    }

    private static class Widget {
        public synchronized void doSomething() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Widget.doSomething");
        }
    }

    private static class LoggingWidget extends Widget {
        @Override
        public synchronized void doSomething() {
            System.out.println("LoggingWidget.doSomething");
            super.doSomething();
        }
    }

    /*public static void main(String[] args) {

     *//*int value = TestEnum.One.getValue() | TestEnum.Two.getValue() | TestEnum.Four.getValue();
        System.out.println(value);
        *//**//*value = value & TestEnum.Two.getValue();
        System.out.println(value);*//**//*
        value = value & TestEnum.Four.getValue();
        System.out.println(value);*//*

     *//*System.out.println("开始");

        test();

        System.gc();

        *//**//*List<String> list = new ArrayList<>();
        int i = 10000;
        while (i > 0) {
            list.add("11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
            i--;
        }*//**//*

        System.out.println("结束");*//*

     *//*List<Integer> numbers = Arrays.asList(5, 2, 1, 8, 6, 3);
     *//**//*numbers.sort((o1, o2) -> {
            if (o1 < o2) {
                return -1;
            } else if (o1 > o2) {
                return 1;
            } else {
                return 0;
            }
        });*//**//*
        numbers = numbers.subList(0, 20);
        System.out.println(numbers);*//*

     *//*LinkedList<Integer> numbers = new LinkedList<>();
        numbers.offer(1);
        numbers.offer(2);
        *//**//*numbers.push(5);
        numbers.offer(3);
        numbers.push(6);
        numbers.offer(4);*//**//*
        System.out.println(numbers);

        Integer pop = numbers.pop();
        System.out.println(pop);
        pop = numbers.pop();
        System.out.println(pop);
        if(numbers.size()>0){
            pop = numbers.pop();
            System.out.println(pop);
        }*//*

     *//*LoggingWidget loggingWidget = new LoggingWidget();

        Thread thread1 = new Thread(() -> {
            loggingWidget.doSomething();
        });

        Thread thread2 = new Thread(() -> {
            loggingWidget.doSomething();
        });

        thread1.start();

        thread2.start();

        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("end");*//*

     *//*double money = Double.valueOf("2.999");
        BigDecimal bigDecimal = BigDecimal.valueOf(money);
        bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();

        System.out.println(0 & 2 & 4);*//*

     *//*MessageFormat.format("", "a", "b");

        MessageFormat.format(null, "a", "b");*//*

     *//*ExecutorService executorService1 = Executors.newSingleThreadExecutor();
        ExecutorService executorService2 = Executors.newSingleThreadExecutor();
        Future<Integer> future = executorService1.submit(() -> {
            System.out.println("开始");
            Future<Integer> future1 = executorService2.submit(() -> {

                for (int i = 0; i < 2000000000; i++) {
                    for (int j = 0; j < 2000000000; j++) {
                        if (j == 100000000 - 1) {

                        }
                    }
                    if (i == 100000000 - 1) {
                        System.out.println("1.循环结束");
                    }
                }
                return 1;
            });

            Future<Integer> future2 = executorService2.submit(() -> {

                for (int i = 0; i < 2000000000; i++) {
                    for (int j = 0; j < 2000000000; j++) {
                        if (j == 100000000 - 1) {

                        }
                    }
                    if (i == 100000000 - 1) {
                        System.out.println("2.循环结束");
                    }
                }
                return 1;
            });

            future1.get();

            future2.get();
            *//**//*try {
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                future2.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*//**//*

            //Thread.sleep(200);
            System.out.println("结束");
            System.out.println("====");
            System.out.println("====");
            System.out.println("====");
            return 1;
        });

        try {
            future.get(1, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
            future.cancel(true);
            System.out.println("取消了");
        }

        try {
            Thread.sleep(5000);
            System.out.println("退出");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*//*

     *//*for (int i = 0; i >= 0; i--) {
            try {
                //int[] arr = new int[Integer.MAX_VALUE-i];
                String str=String.valueOf(new char[Integer.MAX_VALUE-i]);
                System.out.format("Successfully initialized an array with %,d elements.\n", Integer.MAX_VALUE-i);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }*//*

     *//*File file = new File("D:/Users/tjzheng.CN1/Desktop/修改版-101免责协议书v4.docx");
        System.out.println(file.length());
        try {
            byte[] buf = new byte[1024*1024];
            FileInputStream fileInputStream = new FileInputStream(file);
            int totalLen = 0; //文件总大小
            int len = 0;
            while((len = fileInputStream.read(buf)) == buf.length){
                totalLen += buf.length;
            }
            totalLen += len;
            System.out.println(totalLen);

        } catch (Exception e) {
            e.printStackTrace();
        }*//*

     *//*List<String> testStrs = new ArrayList<>();

        testStrs.add("1");
        testStrs.add("1");
        testStrs.add("1");
        testStrs.add("1");
        testStrs.add("1");

        System.out.println("222");


        Collection<String> strings = Collections.unmodifiableCollection(testStrs);

        strings.add("2");

        System.out.println("111");*//*

     *//*ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(() -> {
            *//**//*for (String testStr : testStrs) {
                System.out.println(testStr);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }*//**//*
            Iterator<String> iterator = testStrs.iterator();
            while (iterator.hasNext()){
                String next = iterator.next();
                System.out.println(next);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        testStrs.add("2");

        System.out.println("=========");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*//*

     *//*String test = "台北,台湾,中国";

        System.out.println(Joiner.on(", ").join(test.split(",")));*//*

        List<Integer> numsbers = Lists.newArrayList(2, 5);
        //Integer result = numsbers.stream().reduce((a, b) -> a + b).orElse(10);
        //Integer result = numsbers.stream().reduce(10, (a, b) -> a + b);
        *//*List<String> result = numsbers.stream().reduce(Lists.newArrayList(), (a, b) -> {
            a.add("element-" + b);
            return a;
        }, (a, b) -> null);*//*
        List<String> result = numsbers.parallelStream().reduce(Lists.newArrayList(), (a, b) -> {
            a.add("element-" + b);
            return a;
        }, (a, b) -> {
            a.addAll(b);
            return a;
        });
        System.out.println(result);
    }*/

    private static void test() {
        /*MyHashMap<String, MyEntity> myHashMap = new MyHashMap<>();
        myHashMap.put("111", new MyEntity());
        myHashMap.put(key, new MyEntity());*/

        /*MyHashMap<Object, MyEntity> myHashMap = new MyHashMap<>();
        myHashMap.put(objKey, new MyEntity());
        myHashMap.put("111", new MyEntity());*/

        /*MyHashMap<String, Object> myHashMap = new MyHashMap<>();
        myHashMap.put("111", objVal);*/

        MyEntity myEntity = new MyEntity();
        myEntity.setObj(objVal);
    }

    private static final String key = "222";

    private static final Object objKey = new Object();

    private static final Object objVal = new Object();

    @Autowired
    //@Qualifier("domo1Service1")
    @Qualifier("domo2Biz")
    private IDemoBiz demoBiz;
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private DemoClient demoClient;

    @RequestMapping("/index")
    public String index(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        model.addAttribute("name", name);
        return "demo/index";
    }

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {

        /*Future<?> future = taskExecutor.submit(() -> {
            System.out.println("111");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("222");
        });
        try {
            future.get(100, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            //e.printStackTrace();
        } catch (ExecutionException e) {
            //e.printStackTrace();
        } catch (TimeoutException e) {
            //e.printStackTrace();
            System.out.println("超时");
        }

        System.out.println("333");
        System.out.println("333");
        System.out.println("333");*/


        /*long timeoutWatchStart = System.currentTimeMillis();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long timeoutWatchEnd = System.currentTimeMillis();

        long taketime = timeoutWatchEnd - timeoutWatchStart;*/

        demoClient.hello(new HelloRequestType());

        return demoBiz.hello();
    }

    @ResponseBody
    @RequestMapping("/getUser")
    public UserDto getUser() {
        return demoBiz.getUser();
    }

    @ResponseBody
    @RequestMapping(value = "/saveUser", method = RequestMethod.POST)
    public void saveUser(@Valid UserDto userDto, Errors errors) {
        if (errors.hasErrors()) {
            return;
        }
        demoBiz.saveUser();
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }

    private int indexSeed = 0;

    @ResponseBody
    @RequestMapping("/myServlet")
    public void myServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();

        response.getWriter().write("myServlet indexSeed=" + (++indexSeed));
    }

    @ResponseBody
    @RequestMapping("/singletonTest")
    public String singletonTest() {
        return demoBiz.singletonTest();
    }

    @Autowired
    private IDemoXmlBiz demoXmlBiz;

    @ResponseBody
    @RequestMapping("/instancefromXml")
    public String instancefromXml() {
        return demoXmlBiz.hello();
    }

    @ResponseBody
    @RequestMapping("/extraMethod")
    public String extraMethod() {
        BiggerExtrable biggerExtrable = (BiggerExtrable) demoXmlBiz;
        return biggerExtrable.extraMethod();
    }

    @Autowired
    private ApplicationContext applicationContext;

    @ResponseBody
    @RequestMapping("/resolveTest")
    public String resolveTest() {
        IDemoBiz resolveInstance = applicationContext.getBean("domo1Biz", IDemoBiz.class);
        return resolveInstance.hello();
    }

    @RequestMapping("/myForward")
    public String myForward() {
        return "forward:/demo/index?name=myForward";
    }

    @RequestMapping("/anotherDemo/{id}")
    public String anotherDemo(Map model, @PathVariable(value = "id", required = false) Integer id) {
        model.put("name", "who cares!");
        return "demo/index";
    }

    private static HashMap getSensitiveWordToHashMap(Set<String> keyWordSet) {
        HashMap sensitiveWordMap = new HashMap(keyWordSet.size());     //初始化敏感词容器，减少扩容操作
        String key = null;
        Map nowMap = null;
        Map<String, String> newWorMap = null;
        //迭代keyWordSet
        Iterator<String> iterator = keyWordSet.iterator();
        while (iterator.hasNext()) {
            key = iterator.next();    //关键字
            nowMap = sensitiveWordMap;
            for (int i = 0; i < key.length(); i++) {
                char keyChar = key.charAt(i);       //转换成char型
                Object wordMap = nowMap.get(keyChar);       //获取

                if (wordMap != null) {        //如果存在该key，直接赋值
                    nowMap = (Map) wordMap;
                } else {     //不存在则，则构建一个map，同时将isEnd设置为0，因为他不是最后一个
                    newWorMap = new HashMap<String, String>();
                    newWorMap.put("isEnd", "0");     //不是最后一个
                    nowMap.put(keyChar, newWorMap);
                    nowMap = newWorMap;
                }

                if (i == key.length() - 1) {
                    nowMap.put("isEnd", "1");    //最后一个
                }
            }
        }

        return sensitiveWordMap;
    }

    public static void main(String[] args) {
        /*ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<Integer> future = executorService.submit(() -> {

            System.out.println("=====执行开始=====");
            try {
                System.out.println("=====休眠5秒开始=====");
                Thread.sleep(5000);
                System.out.println("=====休眠5秒结束=====");
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("=====休眠5秒提前结束=====");
                System.out.println("=====子线程执行中断异常=====");
                System.out.println("子线程中断状态：" + Thread.currentThread().isInterrupted());
            }
            System.out.println("=====执行结束=====");

            return 1;
        });*/

        /*ExecutorService executorService1 = Executors.newSingleThreadExecutor();
        ExecutorService executorService2 = Executors.newFixedThreadPool(2);
        Future<Integer> future = executorService1.submit(() -> {

            Future<Integer> future1 = executorService2.submit(() -> {

                System.out.println("1、" + Thread.currentThread().isInterrupted());

                for (int i = 1; i <= 10000; i++) {
                *//*if (Thread.currentThread().isInterrupted()) {
                    System.out.println("子线程中断，提前结束");
                    break;
                }*//*
                    for (int j = 1; j <= 10000; j++) {
                    }
                    if (i == 10000) {
                        System.out.println("future1执行结束");
                    }
                }

                System.out.println("2、" + Thread.currentThread().isInterrupted());
                return 1;
            });

            Future<Integer> future2 = executorService2.submit(() -> {

                System.out.println("3、" + Thread.currentThread().isInterrupted());

                for (int i = 1; i <= 10000; i++) {
                *//*if (Thread.currentThread().isInterrupted()) {
                    System.out.println("子线程中断，提前结束");
                    break;
                }*//*
                    for (int j = 1; j <= 10000; j++) {
                    }
                    if (i == 10000) {
                        System.out.println("future2执行结束");
                    }
                }

                System.out.println("4、" + Thread.currentThread().isInterrupted());
                return 1;
            });

            future1.get();

            System.out.println("111111111");

            future2.get();

            System.out.println("222222222");

            return 1;
        });

        try {
            //future1.get();
            future.get(1, TimeUnit.MILLISECONDS);
            //future2.get(10, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            System.out.println("=====执行异常=====");
            e.printStackTrace();
        } catch (TimeoutException e) {
            future.cancel(true);
            System.out.println("主线程中断状态：" + Thread.currentThread().isInterrupted());
            System.out.println("=====执行超时=====");
            e.printStackTrace();
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("=====退出结束=====");*/

        /*Integer i1 = 1;
        Integer i2 = new Integer(1);
        System.out.println(i1.hashCode() == i2.hashCode());
        System.out.println(i1.equals(i2));*/

        /*String s1 = "abc";
        String s2 = new String("abc");
        System.out.println(s1 == s2);
        System.out.println(s1.hashCode() == s2.hashCode());
        System.out.println(s1.equals(s2));*/

        /*Map<Integer, String> map = new HashMap<>();
        map.put(1, "a");
        map.put(new Integer(1), "b");
        System.out.println(map.size());*/

        /*FutureTask futureTask = new FutureTask<>(() -> {
            System.out.println(111);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(222);
            return 1;
        });
        try {
            futureTask.get(200, TimeUnit.MILLISECONDS);
            System.out.println(333);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
            System.out.println(444);
        }*/
        /*ExecutorService executorService = Executors.newFixedThreadPool(1);
        executorService.submit(() -> {
            ThreadLocalTest.setVal(1);
            System.out.println(Thread.currentThread().getId() + ":" + ThreadLocalTest.getVal());
        });
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getId() + ":" + ThreadLocalTest.getVal());
        });
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getId() + ":" + ThreadLocalTest.getVal());
        });*/

        /*Thread thread = new Thread(() -> {
            try {
                System.out.println(111);
                Thread.sleep(5000);
                System.out.println(222);
            } catch (InterruptedException e) {
                System.out.println("aaaa");
                e.printStackTrace();
            }
        });
        thread.start();
        ScheduledExecutorService cancelExec = Executors.newScheduledThreadPool(1);
        cancelExec.schedule(thread::interrupt, 2000, TimeUnit.MILLISECONDS);
        try {
            thread.join(TimeUnit.MILLISECONDS.toMillis(2000));
            System.out.println("bbbb");
        } catch (InterruptedException e) {
            System.out.println("cccc");
            e.printStackTrace();
        }*/

        /*Lock lock = new ReentrantLock();
        int maxThread = 2;
        ExecutorService executorService = Executors.newFixedThreadPool(maxThread);
        for (int i = 0; i < maxThread; i++) {
            executorService.submit(() -> {
                boolean tryLock = false;
                try {
                    System.out.println("ThreadId:" + Thread.currentThread().getId() + "，开始");
                    //tryLock = lock.tryLock();
                    tryLock = lock.tryLock(3000, TimeUnit.MILLISECONDS);
                    if (tryLock) {
                        System.out.println("ThreadId:" + Thread.currentThread().getId() + "，获取锁");
                        Thread.sleep(5000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if (tryLock) {
                        lock.unlock();
                        System.out.println("ThreadId:" + Thread.currentThread().getId() + "，释放锁");
                    }
                    System.out.println("ThreadId:" + Thread.currentThread().getId() + "，结束");
                }
            });
        }
        executorService.shutdown();*/

        /*ReadWriteMap<String, String> readWriteMap = new ReadWriteMap<>();
        readWriteMap.put("School", "YiZhong");
        int maxThread = 10;
        CyclicBarrier cyclicBarrier = new CyclicBarrier(maxThread + 1);
        ExecutorService executorService = Executors.newFixedThreadPool(maxThread);
        for (int i = 0; i < maxThread; i++) {
            executorService.submit(() -> {
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("ThreadId " + Thread.currentThread().getId() + "：" + readWriteMap.get("School"));
            });
        }
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        readWriteMap.put("School", "ErZhong");
        executorService.shutdown();
        System.out.println("finished");*/

        /*System.out.println(0.3f);
        System.out.println(0.3);
        System.out.println(0.3f == 0.3);
        System.out.println((0.1 * 3));
        System.out.println((0.1 * 3) == 0.3);*/

        /*WeakReference<String> weakReference = new WeakReference<>("test");
        System.out.println(weakReference.get());

        int i = 0;
        while (i < 2) {
            Byte[] bytes = new Byte[600 * 1024 * 1024];
            i++;
            System.gc();
        }


        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(weakReference.get());*/

        HashSet hashSet = new HashSet();
        hashSet.add("中华人民共和国");
        hashSet.add("中华人民共和国少儿");
        hashSet.add("中华人民共和国少先队员");
        HashMap sensitiveWordToHashMap = getSensitiveWordToHashMap(hashSet);
        System.out.println(sensitiveWordToHashMap);
    }

    private static class ReadWriteMap<K, V> {
        private Map<K, V> map;
        private ReadWriteLock readWriteLock;

        public ReadWriteMap() {
            map = new HashMap<>();
            readWriteLock = new ReentrantReadWriteLock();
        }

        public V put(K k, V v) {
            try {
                readWriteLock.writeLock().lock();
                System.out.println("ThreadId " + Thread.currentThread().getId() + "：put " + k + " start");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("ThreadId " + Thread.currentThread().getId() + "：put " + k + " end");
                return map.put(k, v);
            } finally {
                readWriteLock.writeLock().unlock();
            }
        }

        public V get(K k) {
            try {
                readWriteLock.readLock().lock();
                System.out.println("ThreadId " + Thread.currentThread().getId() + "：get " + k + " start");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("ThreadId " + Thread.currentThread().getId() + "：get " + k + " end");
                return map.get(k);
            } finally {
                readWriteLock.readLock().unlock();
            }
        }
    }

    private static class ThreadLocalTest {
        private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

        public static void setVal(Integer val) {
            threadLocal.set(val);
        }

        public static Integer getVal() {
            return threadLocal.get();
        }
    }
}
